[Coding037] LeetCode 82

Remove Duplicates from Sorted List II

Ben 2024.03.29

More coding records

Get the knowledge flowing and circulating! :)

目录

本题收获

题目:82. Remove Duplicates from Sorted List II

Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.

Example 1:

img

Example 2:

img

Constraints:


代码1(配 · 手绘过程图)

image-20240406212707032

image-20240406212722070

代码解读 | 评价

第23~34行非常重要。

这里先判断的是“不相等”的情况,比较巧妙。

第33行是核心。也就是上图中的浅蓝色线条指向的动作:实现了去重的操作。

复杂度分析

image-20240406213110686